package de.connecttext.deprecated;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import de.connecttext.application.Constants;
import de.connecttext.dao.solr.SolrDao;
import de.connecttext.model.MetaData;
import de.connecttext.model.TagVector;
import de.connecttext.model.TextDocument;
import de.connecttext.services.MetaDataExtractor;
import de.connecttext.services.TagSelectAndRefiner;

/**
 * Schicht, um mit dem SolrServlet zu kommunizieren und für jeden Term die
 * geünschten statistischen Informationen zu erhalten. Das passende Solr-Servlet
 * muss die SearchComponent TermVectorComponent aktiviert haben (siehe Doku).
 * Das zu untersuchende Feld (meist das plaintextFeld in das alles gedumpt wird)
 * 
 * Singleton!
 * 
 * @author conrad
 * 
 */
public class GutenbergSolrDao extends SolrDao {

	private static GutenbergSolrDao instance = null;

	private String hostName = "localhost";
	private String servletName = "solr";
	private String requestHandlerName = "request";
	private String dataimportHandlerName = "dataimport";
	// Name of the field containing all the text which is to be analyzed
	private String fieldName = "metaField";



	private GutenbergSolrDao() {

	}

	/**
	 * Es wird ein Document mit dem wichtigsten Terms nach tfidf von solr
	 * abgefragt
	 * 
	 * @param row
	 *            Welches Dokument wird returned?
	 * @throws IOException
	 */
	public TextDocument get(int row)  {
		TextDocument toReturn = new TextDocument();

		String path= null;
		try {
			path = retrieveSolrId(row);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		MetaData metaData = new MetaData();
		try {
			metaData.setAuthor(retrieveMetaDataFragment(path, "Author:"));
			metaData.setTitle(retrieveMetaDataFragment(path, "Title:"));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("folgende Metadaten gefunden: "
				+ metaData.getAuthor() + " " + metaData.getTitle());
		List<TagVector> tagVectors = null;
		try {
			tagVectors = retrieveTagVectors(path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//tagVectors = (new TagSelector()).selectTags(tagVectors);
		/*
		 * System.out.println(metaData.getTitle() + ": " + tagVectors.size() +
		 * " Tags: ");
		 * 
		 * for (TagVector tagVector : tagVectors) {
		 * System.out.println(tagVector.toString()); }
		 */
		toReturn.setSolrId(path);
		toReturn.setMetaData(metaData);
		toReturn.setTagVectors(tagVectors);
		//System.out.println("Document successfully read from solr: " + path);
		return toReturn;

	}





	/**
	 * tries to extract Metadata from the Document. In the Current Version this
	 * relies heavily on the Textfilestructure as offered by Project Gutenberg.
	 * Future implementation should feature a more modular structure to
	 * acknowledge other formats also
	 * 
	 * @param id
	 *            specifies which Document is to be retrieved
	 * 
	 * @return MetaData Object
	 * @throws IOException
	 */
	private String retrieveMetaDataFragment(String id, String fragment)
			throws IOException {
		String urlString = new String("select?q=metaField:\"" + fragment + "\"&fq=id:" + id
				+ "&start=0&rows=1&fl=id" + "&wt=json&hl=true&hl.fragsize=150");
		String responseString = urlRequest(urlString);
		JSONObject root = (JSONObject) JSONValue.parse(responseString);
		JSONObject response = (JSONObject) root.get("response");
		long numFound = (Long) response.get("numFound");
		// System.out.println(numFound + " " + url);

		// intantiate empty MetaData
		String toReturn = null;

		if (numFound > 0) {
			JSONObject highlighting = (JSONObject) root.get("highlighting");
			if (highlighting.containsKey(id)) {
				JSONObject idObject = (JSONObject) highlighting.get(id);
				if (idObject.containsKey(fieldName)) {
					JSONArray fieldNameArray = (JSONArray) idObject
							.get(fieldName);
					if (fieldNameArray.size() > 0) {
						String snippet = (String) fieldNameArray.get(0);

						toReturn = (new MetaDataExtractor())
								.extractMetaDataFragmentFromGutenBergSnippet(snippet);
					}
				}
			}

		} else {
			System.out.println(fragment
					+ "wurde nicht gefunden in diesem Document ");
			System.out.println(urlString);
		}

		return toReturn;
	}


	



	public static GutenbergSolrDao getInstance() {
		if (instance == null)
			return new GutenbergSolrDao();
		else
			return instance;
	}

}
